﻿@inherits RadzenComponent

@if (Visible)
{
    <button @ref="@Element" style="@Style" disabled="@IsDisabled"
            type="@Enum.GetName(typeof(ButtonType), ButtonType).ToLower()"
            @attributes="Attributes" class="@GetCssClass()" id="@GetId()"
            @onclick="@((args) => OnClick(args))">
        @if (ChildContent != null)
        {
            @ChildContent
        }
        else
        {
            @if (IsBusy)
            {
                <RadzenIcon Icon="refresh" Style="animation: rotation 700ms linear infinite" />
                @if (!string.IsNullOrEmpty(BusyText))
                {
                    <span class="rz-button-text">@BusyText</span>
                }
            }
            else
            {
                @if (!string.IsNullOrEmpty(@Icon))
                {
                    <i class="rz-button-icon-left rzi">@((MarkupString)Icon)</i>
                }
                @if (!string.IsNullOrEmpty(Image))
                {
                    <img class="rz-button-icon-left rzi" src="@Image" />
                }
                @if (!string.IsNullOrEmpty(Text))
                {
                    <span class="rz-button-text">@Text</span>
                }
            }
        }
    </button>
}
@code {
    private string getButtonSize()
    {
        return Size == ButtonSize.Medium ? "md" : "sm";
    }

    [Parameter]
    public RenderFragment ChildContent { get; set; }

    [Parameter]
    public string Text { get; set; } = "";

    [Parameter]
    public string Icon { get; set; }

    [Parameter]
    public string Image { get; set; }

    [Parameter]
    public ButtonStyle ButtonStyle { get; set; } = ButtonStyle.Primary;

    [Parameter]
    public ButtonType ButtonType { get; set; } = ButtonType.Button;

    [Parameter]
    public ButtonSize Size { get; set; } = ButtonSize.Medium;

    [Parameter]
    public bool Disabled { get; set; }

    [Parameter]
    public EventCallback<MouseEventArgs> Click { get; set; }

    [Parameter]
    public bool IsBusy { get; set; }

    [Parameter]
    public string BusyText { get; set; } = "";

    public bool IsDisabled { get => Disabled || IsBusy; }

    bool clicking;
    public async Task OnClick(MouseEventArgs args)
    {
        if (clicking)
        {
            return;
        }

        try
        {
            clicking = true;

            await Click.InvokeAsync(args);
        }
        finally
        {
            clicking = false;
        }
    }

    protected override string GetComponentCssClass()
    {
        return $"rz-button rz-button-{getButtonSize()} btn-{Enum.GetName(typeof(ButtonStyle), ButtonStyle).ToLower()}{(IsDisabled ? " rz-state-disabled" : "")}{(string.IsNullOrEmpty(Text) && !string.IsNullOrEmpty(Icon) ? " rz-button-icon-only" : "")}";
    }
}
